package com.turing.post.salesmanage.impl;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.turing.post.common.util.SysOperLog;
import com.turing.post.common.util.SysOperLogDao;
import com.turing.post.salesmanage.util.DepositOrder;
import com.turing.post.salesmanage.util.DepositOrderDao;
import com.turing.post.salesmanage.util.DepositOrderSalesDetail;
import com.turing.post.salesmanage.util.DepositOrderSalesDetailBusiness;
import com.turing.post.salesmanage.util.DepositOrderSalesDetailDao;
import com.turing.post.salesmanage.util.SalesManageApplicationException;
import com.turing.post.salesmanage.util.SalesManageRespCode;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSWebInput;

/**
 * 充值单销售员明细（DepositOrderSalesDetail）
 * 
 * @author zxp
 * @Jan 17, 2013
 */
@Stateless(mappedName = "salesmanage.impl.DepositOrderSalesDetailBusinessImpl")
@Remote
@Local
public class DepositOrderSalesDetailBusinessImpl implements
		DepositOrderSalesDetailBusiness {

	@EJB(mappedName = "salesmanage.impl.DepositOrderSalesDetailDaoImpl")
	private DepositOrderSalesDetailDao depOrderDaoImpl;

	@EJB(mappedName = "salesmanage.impl.DepositOrderDaoImpl")
	private DepositOrderDao depositOrderDaoImpl;

	@EJB(mappedName = "common.impl.SysOperLogDaoImpl")
	private SysOperLogDao sysOperLogDaoImpl;

	/**
	 * 异常抛出方法
	 * 
	 * @param error
	 * @throws PCPSApplicationException
	 */
	private void errException(String error) throws PCPSApplicationException {
		throw new SalesManageApplicationException(
				SalesManageApplicationException.ERRCODE_CATEGORY_APPLICATION,
				SalesManageRespCode.DEPOSIT_EXCEPTIONCODE, error);
	}

	// 删除
	@Override
	public void deleteDepositOrderSalesDetailInfo(PCPSWebInput webInput,
			DepositOrderSalesDetail dosd) throws PCPSApplicationException {
		DepositOrderSalesDetail depositOSD = depOrderDaoImpl
				.getDepositOrderSalesDetailByPrimaryKey(dosd);
		if (depositOSD != null) {
			// 判断充值单状态,只有录入状态才可执行
			DepositOrder _depositOrder = depositOrderDaoImpl
					.getDepositOrderByDepositOrderId(depositOSD
							.getDepositOrderId());
			if (!_depositOrder.getOrderStatus().equals(
					DepositOrder.DEPORDER_ORDERSTATUS_INPUT)) {
				errException("只允许在录入状态下删除销售员!");
			}
			// 日志记录
			sysOperLogDaoImpl.insertSysOperLogInfo(webInput,
					SysOperLog.operType_Delete, "删除充值单销售员明细", "DepositOrderId:"
							+ depositOSD.getDepositOrderId());
			depOrderDaoImpl.deleteDepositOrderSalesDetail(depositOSD);
		}
	}

	// 新增
	@Override
	public void insertDepositOrderSalesDetailInfo(PCPSWebInput webInput,
			DepositOrderSalesDetail dosd) throws PCPSApplicationException {
		// 业务判断
		if (dosd == null) {
			errException("添加销售员对象为空!");
		}
		// 双主键
		if (dosd.getSalesId() == null || dosd.getDepositOrderId() == null) {
			errException("销售员编号或者充值单编号为空!");
		}

		if (depOrderDaoImpl.getDepositOrderSalesDetailByPrimaryKey(dosd) != null) {
			errException("该充值单下销售员已经添加!");
		}

		// 录入销售员明细时判断充值单状态,只有录入状态才可添加
		DepositOrder _depositOrder = depositOrderDaoImpl
				.getDepositOrderByDepositOrderId(dosd.getDepositOrderId());
		if (!_depositOrder.getOrderStatus().equals(
				DepositOrder.DEPORDER_ORDERSTATUS_INPUT)) {
			errException("只允许在录入状态下添加销售员信息!");
		}
		// 新增充值单销售员
		depOrderDaoImpl.insertDepositOrderSalesDetail(dosd);
		// 日志记录
		sysOperLogDaoImpl.insertSysOperLogInfo(webInput,
				SysOperLog.operType_Delete, "新增删除充值单销售员明细", "DepositOrderId:"
						+ dosd.getDepositOrderId());
	}
}